package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhang.mapper.ISupplierMapper;
import com.zhang.pojo.SupplierBean;
import com.zhang.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;

    @Override
    public IPage<SupplierBean> findByPageAndItem(Page<SupplierBean> page, String name, String scope) {
        UpdateWrapper<SupplierBean> wrapper = new UpdateWrapper<>();
        if (!StringUtils.isNullOrEmpty(name)){
            wrapper.like("s_name",name);
        }if (!StringUtils.isNullOrEmpty(scope)){
            wrapper.like("s_scope",scope);
        }
        return  supplierMapper.selectPage(page,wrapper);
    }

    @Override
    public List<SupplierBean> findAll() {
        return supplierMapper.selectList(null);
    }

    @Override
    public void add(SupplierBean supplierBean) {
        supplierMapper.insert(supplierBean);
    }

    @Override
    public SupplierBean findById(int id) {
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("pk_supplierId",id);
        return supplierMapper.selectById(id);
    }

    @Override
    public void update(SupplierBean supplierBean) {
        UpdateWrapper<SupplierBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("pk_supplierId",supplierBean.getId());
        supplierMapper.update(supplierBean,wrapper);
    }
}
